﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;

namespace ExpertMIS.Admin.AdminTools
{
    public partial class RunSQLScript : System.Web.UI.Page
    {
        private void LoadSQLScript()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(ConfigurationManager.AppSettings["ScriptDir"].ToString()));
            foreach (FileInfo fileInfo in dirInfo.GetFileSystemInfos())
            {
                if (fileInfo.Extension.ToLower() != ".sql") continue;
                string display = String.Format("{0},Create: {1}, LastModify: {2}, Size: {3} bytes",
                    fileInfo.Name,
                    fileInfo.CreationTime,
                    fileInfo.LastAccessTime,
                    fileInfo.Length);
                string value = fileInfo.Name;
                ListItem item = new ListItem(display, value);
                ddlScriptFiles.Items.Add(item);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            using (SqlConnection cnn = new SqlConnection(txConnStr.Text))
            {
                try
                {
                    cnn.Open();
                    SqlCommand cmd = new SqlCommand(txScript.Text, cnn);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                    trErrorInfo.Visible = true;
                }
                finally
                {
                    cnn.Close();
                }
            }
        }
    }
}
